//
//  FeedsCell.swift
//  HinsLive
//
//  Created by haoxian on 2019/4/18.
//  Copyright © 2019 Hins. All rights reserved.
//

import UIKit
import Kingfisher


class FeedsCell: UICollectionViewCell {
    
    private lazy var snapshotImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.clipsToBounds = true
        imageView.layer.cornerRadius = 8
        imageView.backgroundColor = .imagePlaceholderColor
        imageView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(FeedsCell.screenShotDidTap))
        imageView.addGestureRecognizer(tap)
        return imageView
    }()
    
    private lazy var avatarImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.layer.cornerRadius = 36.0/2
        imageView.backgroundColor = .imagePlaceholderColor
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Helvetica-Bold", size: 15)
        label.textColor = .white
        label.textAlignment = .left
        return label
    }()
    
    private lazy var roomTitleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(name: "Helvetica-Bold", size: 12)
        label.textColor = .white
        label.textAlignment = .left
        return label
    }()
    
    private(set) var room: RoomInfo?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        snapshotImageView.image = nil
        avatarImageView.image = nil
        nameLabel.text = nil
        roomTitleLabel.text = nil
        snapshotImageView.backgroundColor = .imagePlaceholderColor
    }
    
    func configData(with feedItem: FeedItem) {
        avatarImageView.kf.setImage(with: URL(string: feedItem.avatarURL), options: [.backgroundDecode])
        snapshotImageView.kf.setImage(with: URL(string: feedItem.snapshotURL), options: [.backgroundDecode]) { [weak self] (result) in
            if case .success(let value) = result {
                self?.snapshotImageView.backgroundColor = .black
                if value.image.size.width >= value.image.size.height {
                    self?.snapshotImageView.contentMode = .scaleAspectFit
                } else {
                    self?.snapshotImageView.contentMode = .scaleAspectFill
                }
            }
        }
        nameLabel.text = feedItem.name
        roomTitleLabel.text = feedItem.roomTitle
        room = RoomInfo(ruid: feedItem.ruid,
                        roomTitle: feedItem.roomTitle,
                        fid: feedItem.fid,
                        name: feedItem.name,
                        avatarURL: feedItem.avatarURL)
    }
}

private extension FeedsCell {
    func setupSubviews() {
        contentView.backgroundColor = .clear
        addSubview(snapshotImageView)
        snapshotImageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        snapshotImageView.addSubview(avatarImageView)
        avatarImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(36)
            make.left.equalToSuperview().offset(5)
            make.top.equalTo(snapshotImageView.snp.bottom).offset(-60)
        }
        
        snapshotImageView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(avatarImageView)
            make.height.equalTo(36)
            make.left.equalTo(avatarImageView.snp.right).offset(5)
            make.right.equalTo(snapshotImageView.snp.right).offset(-5)
        }
        
        snapshotImageView.addSubview(roomTitleLabel)
        roomTitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(snapshotImageView.snp.left).offset(5)
            make.right.equalTo(snapshotImageView.snp.right).offset(-5)
            make.bottom.equalToSuperview()
            make.top.equalTo(avatarImageView.snp.bottom)
        }
    }
    
    @objc func screenShotDidTap() {
        guard let model = room else { return }
        let topVC = ViewController.topViewController()
        let liveVc = AudienceLiveViewController(roomModel: model)
        topVC.present(liveVc, animated: true)
    }
}

